package com.caiyigo.baihua.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.caiyigo.baihua.dto.KaoshicjDto;
import com.caiyigo.baihua.dto.KaoshizwDto;
import com.caiyigo.baihua.model.Kaoshicj;

public class GetThread implements Callable<List<Object>> {
	Logger logger = Logger.getLogger(GetThread.class);
	private final HttpClient httpClient;
	private final HttpContext context;
	private final HttpGet httpget;
	private final boolean isZw;
	private final String zkz;
	private final String pici;

	public GetThread(HttpClient httpClient, HttpGet httpget, boolean isZw,String zkz,String pici) {
		this.httpClient = httpClient;
		this.context = new BasicHttpContext();
		this.httpget = httpget;
		this.isZw = isZw;
		this.zkz = zkz;this.pici = pici;
	}

	// @Override
	// public void run() {
	// this.setName("threadsPoolClient");
	// get();
	// }

	public List<Object> get() {
		List<Object> list = new ArrayList<Object>();
		try {
			HttpResponse response = this.httpClient.execute(this.httpget,
					this.context);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				Document document = Jsoup.parse(EntityUtils.toString(entity),
						"GBK");
				Elements trs = document.select("tr");

				if (trs.size() > 0) {
					if (isZw) {

						for (int i = 1; i < trs.size() - 2; i++) {
							System.out.println("有座位的");
							Elements tds = null;
							tds = trs.get(i).select("td");
							int totalTds = tds.size();
							KaoshizwDto zw = new KaoshizwDto();
							zw.setZkz(zkz);
							zw.setPici(pici);
							for (int j = 0; j < totalTds; j++) {
								switch (j) {
								case 0:
									// 姓名：郭文可
									zw.setName(tds.get(j).html().toString());
									System.out.println(tds.get(j).html()
											.toString());
									break;
								case 1:
									// 科目：西方政治制度
									zw.setKemu(tds.get(j).html().toString());
									System.out.println(tds.get(j).html()
											.toString());
									break;
								case 2:
									// 考场：
									zw.setKaochang(tds.get(j).html().toString());
									System.out.println(tds.get(j).html()
											.toString());

									break;
								case 3:
									// 地址：
									zw.setDizhi(tds.get(j).html().toString());
									System.out.println(tds.get(j).html()
											.toString());
									break;
								case 4:
									// 试室
									zw.setShishi(tds.get(j).html().toString());
									System.out.println(tds.get(j).html()
											.toString());
									break;
								case 5:
									// 座位号
									zw.setZuoweihao(tds.get(j).html()
											.toString());
									System.out.println(tds.get(j).html()
											.toString());
									break;
								case 6:
									// 考试时间
									zw.setShijian(tds.get(j).html()
											.toString());
									System.out.println(tds.get(j).html()
											.toString());
									break;
								case 7:
									// 答题卡
									zw.setDatika(tds.get(j).html().toString());
									System.out.println(tds.get(j).html()
											.toString());
									break;
								default:
									break;
								}
							}
							list.add(zw);
						}
					} else {
						for (int i = 1; i < trs.size() - 2; i++) {
							System.out.println("有成绩的");
							KaoshicjDto cj = new KaoshicjDto();
							cj.setPici(pici);
							Elements tds = null;
							tds = trs.get(i).select("td");
							int totalTds = tds.size();
							for (int j = 0; j < totalTds; j++) {
								switch (j) {
								case 0:
									// 010414102012
									System.out.println(tds.get(j).html()
											.toString());
									cj.setZkz(tds.get(j).html().toString());
									break;
								case 1:
									// 莫健儿

									cj.setName(tds.get(j).html().toString());
									System.out.println(tds.get(j).html()
											.toString());
									break;
								case 2:
									// 03005
									cj.setKmid(tds.get(j).html().toString());
									System.out.println(tds.get(j).html()
											.toString());
									break;
								case 3:
									// 护理教育导论
									cj.setKmmc(tds.get(j).html().toString());
									System.out.println(tds.get(j).html()
											.toString());
									break;
								case 4:
									// 80
									cj.setCjfs(tds.get(j).html().toString());
									System.out.println(tds.get(j).html()
											.toString());
									break;
								default:
									break;

								}
							}
							list.add(cj);
						}

					}

				} else {
					System.out.println("没成绩的");
				}
			}
			// ensure the connection gets released to the manager
			EntityUtils.consume(entity);
		} catch (Exception ex) {
			System.out.println("异常：" + ex.getMessage());
			this.httpget.abort();
		} finally {
			httpget.releaseConnection();
		}
		return list;
	}

	@Override
	public List<Object> call() throws Exception {
		// TODO Auto-generated method stub
		return get();
	}
}